/*
 * @Author: asushiye
 * @Date: 2020-07-07 09:41:13
 * @Commit: wechatkey, notice, message
 * 2022-01-05: 新增模式窗口提示函数
 */

import { EventEmitter, Injectable, Injector, TemplateRef } from '@angular/core';
import { ActivatedRoute, NavigationExtras, Params, Router, UrlTree } from '@angular/router';
import { environment } from '@env/environment';
import { NzMessageDataOptions, NzMessageRef, NzMessageService } from 'ng-zorro-antd/message';
import { ConfirmType, NzModalService, OnClickCallback } from 'ng-zorro-antd/modal';
import { NzNotificationDataOptions, NzNotificationRef, NzNotificationService } from 'ng-zorro-antd/notification';
import { NzSanitizerPipe } from 'ng-zorro-antd/pipes';

@Injectable({
  providedIn: 'root'
})
export class AfMessageService {
  constructor(
    private injector: Injector,
    private msgService: NzMessageService,
    private router: Router,
    private actiateRoute: ActivatedRoute,
    private modalSrv: NzModalService
  ) {}

  public get notification(): NzNotificationService {
    return this.injector.get(NzNotificationService);
  }

  // console 输出, 用于开发调试
  public console_info(message?: any, enforce: boolean = false, ...optionalParams: any[]) {
    if (!environment.production || enforce) {
      console.info(message, optionalParams);
    }
  }
  public console_debug(message?: any, enforce: boolean = false, ...optionalParams: any[]) {
    if (!environment.production || enforce) {
      console.debug(message, optionalParams);
    }
  }
  public console_error(message?: any, enforce: boolean = false, ...optionalParams: any) {
    if (!environment.production || enforce) {
      console.error(message, optionalParams);
    }
  }

  // 通知提醒框, 右上边显示，默认显示时间3s
  public notice_success(title: string, content: string, options?: NzNotificationDataOptions): NzNotificationRef {
    return this.notification.success(title, content, options);
  }
  public notice_error(title: string, content: string, options?: NzNotificationDataOptions): NzNotificationRef {
    return this.notification.error(title, content, options);
  }
  public notice_info(title: string, content: string, options?: NzNotificationDataOptions): NzNotificationRef {
    return this.notification.info(title, content, options);
  }
  public notice_warning(title: string, content: string, options?: NzNotificationDataOptions): NzNotificationRef {
    return this.notification.warning(title, content, options);
  }
  public notice_blank(title: string, content: string, options?: NzNotificationDataOptions): NzNotificationRef {
    return this.notification.blank(title, content, options);
  }

  // 全局提示消息，中间显示，默认显示时间3s
  public msg_success(content: string, options?: NzMessageDataOptions): NzMessageRef {
    return this.msgService.success(content, options);
  }
  public msg_error(content: string, options?: NzMessageDataOptions): NzMessageRef {
    return this.msgService.error(content, options);
  }
  public msg_info(content: string, options?: NzMessageDataOptions): NzMessageRef {
    return this.msgService.info(content, options);
  }
  public msg_warning(content: string, options?: NzMessageDataOptions): NzMessageRef {
    return this.msgService.warning(content, options);
  }
  public msg_loading(content: string, options?: NzMessageDataOptions): NzMessageRef {
    return this.msgService.loading(content, options);
  }

  //弹出对话框
  //nzContent 支持 <p> 标签
  // 用法演示
  // this.msgSrv.form_confirm(
  //   '请确定是否真的要清空？',
  //   '清空请点击“确定”',
  //   () => {
  //     console.log('OK');
  //   },
  //   () => {}
  // );
  form_confirm<T>(
    nzTitle?: string | TemplateRef<{}>,
    nzContent?: string | TemplateRef<{}>,
    okClick?: EventEmitter<T> | OnClickCallback<T>,
    cancelClick?: EventEmitter<T> | OnClickCallback<T>
  ): void {
    this.modalSrv.confirm({
      nzTitle: nzTitle,
      nzContent: nzContent,
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOnOk: okClick,
      nzCancelText: '取消',
      nzOnCancel: cancelClick
    });
  }

  form_confirm2<T>(
    nzTitle?: string | TemplateRef<{}>,
    nzContent?: string | TemplateRef<{}>,
    okClick?: EventEmitter<T> | OnClickCallback<T>,
    cancelClick?: EventEmitter<T> | OnClickCallback<T>,
    width?: string | number | undefined,
    confirmType?: ConfirmType | undefined,
    nzOkText: string = '确定',
    nzCancelText: string = '取消'
  ): void {
    this.modalSrv.confirm(
      {
        nzTitle: nzTitle,
        nzContent: nzContent,
        nzWidth: width,
        nzOkText: nzOkText,
        nzOkType: 'primary',
        nzOnOk: okClick,
        nzCancelText: nzCancelText,
        nzOnCancel: cancelClick
      },
      confirmType
    );
  }

  form_delete<T>(
    nzTitle?: string | TemplateRef<{}>,
    nzContent?: string | TemplateRef<{}>,
    okClick?: EventEmitter<T> | OnClickCallback<T>,
    cancelClick?: EventEmitter<T> | OnClickCallback<T>
  ): void {
    this.modalSrv.confirm({
      nzTitle: nzTitle,
      nzContent: nzContent,
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: okClick,
      nzCancelText: '取消',
      nzOnCancel: cancelClick
    });
  }

  form_info(title: any, content: any): void {
    this.modalSrv.info({
      nzTitle: title,
      nzContent: content
    });
  }

  form_success(title: any, content: any): void {
    this.modalSrv.success({
      nzTitle: title,
      nzContent: content
    });
  }

  form_error(title: any, content: any): void {
    this.modalSrv.error({
      nzTitle: title,
      nzContent: content
    });
  }

  form_warning(title: any, content: any): void {
    this.modalSrv.warning({
      nzTitle: title,
      nzContent: content
    });
  }

  // 路由功能
  navigateByUrl(url: string | UrlTree, extras?: NavigationExtras): Promise<boolean> {
    return this.router.navigateByUrl(url, extras);
  }
  navigate(commands: any[], extras?: NavigationExtras): Promise<boolean> {
    return this.router.navigate(commands, extras);
  }
  // 获取路由查询参数
  getQueryParam(): Params {
    return this.actiateRoute.snapshot.queryParams;
  }
}
